#!/bin/sh

# Slackware build script for imv

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

PRGNAM=imv
VERSION=${VERSION:-4.1.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# libtiff complains "not a TIFF image" for every non-TIFF image imv
# loads. patch disables libtiff warnings, and also moves libtiff to
# the end of the backend list, so it'll get tried last (after all the
# other backends have decided they can't handle the file).
patch -p1 < $CWD/squelch_libtiff_warnings.diff

# missing an include...
sed -i '1i#include <string.h>' src/backend_freeimage.c

# look in the right place for the config file.
sed -i 's,/usr/local/etc/,/etc/imv/,' src/imv.c

# No autodetection for optional deps. Help it out a little.
# libpng, libtiff, libjpeg-turbo are always included.
# librsvg never included, 14.2's is too old, and the minimum supported
# version requires rust, meaning I don't love it enough to include a
# static build here. I'm not just assuming it's too old because the
# docs say so, I actually tried it. Sigh.
W=x11; F=no; G=no; WS=no
pkg-config --exists wayland-client && W=all && WS=yes
[ -e /usr/include/FreeImage.h ] && F=yes
pkg-config --exists libnsgif && G=yes

cat >config.mk <<EOF
# Core Slackware stuff:
BACKEND_LIBTIFF=yes
BACKEND_LIBPNG=yes
BACKEND_LIBJPEG=yes

# Core Slackware 14.2, but too old to use, sorry.
BACKEND_LIBRSVG=no

# Optional SBo packages (FreeImage, libnsgif):
BACKEND_FREEIMAGE=$F
BACKEND_LIBNSGIF=$G

# Optional: window system (x11, wayland, or 'all' for both)
WINDOWS=$W
EOF

make \
  CFLAGS="$SLKCFLAGS" \
  PREFIX=/usr \
  CONFIGPREFIX=/etc/$PRGNAM

make install \
  DESTDIR=$PKG \
  PREFIX=/usr \
  CONFIGPREFIX=/etc/$PRGNAM \
  MANPREFIX=/usr/man \
  INSTALL_PROGRAM="install -s -m0755"

gzip -9 $PKG/usr/man/man?/*.?

( cd $PKG/etc/$PRGNAM; mv ${PRGNAM}_config ${PRGNAM}_config.new )

# make install doesn't install the .desktop file. Also there's no icon
# so we have to include one, below.
mkdir -p $PKG/usr/share/applications
sed "s,multimedia-photo-viewer,$PRGNAM," files/$PRGNAM.desktop > \
    $PKG/usr/share/applications/$PRGNAM.desktop

# Icon came from Debian moka-icon-theme package. I have no idea
# what the default icon theme is in Debian (equivalent of hicolor in
# Slackware). Don't care, either. So I picked one at random.
for i in 16 22 24 48 64 96; do
  mkdir -p $PKG/usr/share/icons/hicolor/${i}x${i}/apps
  cat $CWD/icons/$i.png > $PKG/usr/share/icons/hicolor/${i}x${i}/apps/$PRGNAM.png
done
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS* CHANGE* CONTRIB* LICENSE* README* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install

sed -e "s,@F@,$F," \
    -e "s,@G@,$G," \
    -e "s,@W@,$WS," \
    $CWD/slack-desc > $PKG/install/slack-desc

cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
